/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.domain.IllnessSymptom;
import com.oyun.modules.medicine.domain.Symptom;
import com.oyun.modules.medicine.domain.vo.SymptomQueryCriteria;
import com.oyun.modules.medicine.mapper.IllnessSymptomMapper;
import com.oyun.modules.medicine.mapper.SymptomMapper;
import com.oyun.modules.medicine.service.SymptomService;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @description 服务实现
* @author oyun
* @date 2024-05-07
**/
@Service
@RequiredArgsConstructor
public class SymptomServiceImpl extends ServiceImpl<SymptomMapper, Symptom> implements SymptomService {

    private final SymptomMapper symptomMapper;

    private final IllnessSymptomMapper illnessSymptomMapper;

    @Override
    public PageResult<Symptom> queryAll(SymptomQueryCriteria criteria, Page<Object> page){
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        IPage<Symptom> all = symptomMapper.findAll(criteria, page);
        Acquire(all.getRecords());
        return PageUtil.toPage(all);
    }

    @Override
    public List<Symptom> queryAll(SymptomQueryCriteria criteria){
        return symptomMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Symptom resources) {
        convertCode(resources);
        save(resources);
        for (Long illnessId : resources.getIllnessIds()){
            IllnessSymptom symptom = new IllnessSymptom();
            symptom.setIllnessId(illnessId);
            symptom.setSymptomId(resources.getId());
            symptom.setStatus(1);
            illnessSymptomMapper.insert(symptom);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Symptom resources) {
        Symptom symptom = getById(resources.getId());
        symptom.copy(resources);
        convertCode(symptom);
        saveOrUpdate(symptom);
        LambdaQueryWrapper<IllnessSymptom> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IllnessSymptom::getSymptomId,resources.getId());
        List<IllnessSymptom> list = illnessSymptomMapper.selectList(wrapper);
        for (IllnessSymptom illnessSymptom : list){
            illnessSymptom.setStatus(0);
            illnessSymptomMapper.updateById(illnessSymptom);
        }
        for (Long illnessId : resources.getIllnessIds()){
            IllnessSymptom symptom1 = new IllnessSymptom();
            symptom1.setIllnessId(illnessId);
            symptom1.setSymptomId(resources.getId());
            symptom1.setStatus(1);
            illnessSymptomMapper.insert(symptom1);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
        LambdaQueryWrapper<IllnessSymptom> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(IllnessSymptom::getSymptomId,ids).eq(IllnessSymptom::getSymptomId,1);
        List<IllnessSymptom> list = illnessSymptomMapper.selectList(wrapper);
        for (IllnessSymptom illnessSymptom : list){
            illnessSymptom.setStatus(0);
            illnessSymptomMapper.updateById(illnessSymptom);
        }
    }

    @Override
    public void download(List<Symptom> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Symptom symptom : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" mnSymptom",  symptom.getMnSymptom());
            map.put(" mnSymptomCode",  symptom.getMnSymptomCode());
            map.put(" zhSymptom",  symptom.getZhSymptom());
            map.put(" createBy",  symptom.getCreateBy());
            map.put(" createTime",  symptom.getCreateTime());
            map.put(" updateBy",  symptom.getUpdateBy());
            map.put(" updateTime",  symptom.getUpdateTime());
            map.put(" remark",  symptom.getRemark());
            map.put(" status",  symptom.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Object getByName(String name) {
        String replaceAll = IntermediateCodeUtils.mn2Code(name).replaceAll("-", " -");
        LambdaQueryWrapper<Symptom> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Symptom::getZhSymptom,name).or().like(Symptom::getMnSymptomCode,replaceAll);
        List<Symptom> selectList = symptomMapper.selectList(wrapper);
        Acquire(selectList);
        return selectList;
    }

    private void convertCode(Symptom symptom){
        symptom.setMnSymptomCode(IntermediateCodeUtils.mn2Code(symptom.getMnSymptom()).replaceAll("-", " -"));
    }


    private void Acquire(List<Symptom> symptomList){
        for (Symptom symptom : symptomList){
            LambdaQueryWrapper<IllnessSymptom> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(IllnessSymptom::getSymptomId,symptom.getId()).eq(IllnessSymptom::getStatus,1);
            List<IllnessSymptom> illnessSymptoms = illnessSymptomMapper.selectList(wrapper);
            List<Long> collect = illnessSymptoms.stream().map(IllnessSymptom::getIllnessId).collect(Collectors.toList());
            if (collect != null) symptom.setIllnessIds(collect);
        }
    }
}
